package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.service.DapanService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * 小程序访问数据
 * @author xiyong.lxy
 * @date 2020-10-12 16:51
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/dapan")
public class DapanController2 {
    @Autowired
    private DapanService dapanService;


    @Data
    public static class Param {
        public String dt;
        public String dt2;
        /***/
        public String biz;

        public String appDiv;
        public String zgroup;
        public String pageName;
        public String bizArea;
        public String metaIds;
        public String id;
        public String ids;
    }

    /**
     *  [小程序使用] 大盘展示
     * @param params 请求参数  dt时间， dt2结束时间， biz业务: [zp,ganji,chinahr]
     * @param result 请求结果
     * @return 状态返回
     */
//    @RestGrpcMapping("/getYeWuResult2.do")
//    public NTSTATUS getYeWuResult2(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("request params: {}", params);
//        try {
//            Param aa = params.driverObject(Param.class);
//            result.setData(dapanService.getYeWuResult2(aa.dt, aa.dt2, aa.biz, params.userID));
//            return NTSTATUS.SUCCESS_STATUS();
//        }catch (BusinessException e) {
//            log.warn("Exception: {}. {}",e.toString(), params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        }catch (Exception e) {
//            log.error("Exception: {}. {}", e.toString(), params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }

    /**
     *  [小程序使用] 大盘展示
     * @param params 请求参数  dt时间， dt2结束时间， biz业务: [zp,ganji,chinahr], id 具体指标
     * @param result 请求结果
     * @return 状态返回
     */
    @RestGrpcMapping("/getYeWuResult3.do")
    public NTSTATUS getYeWuResult3(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            result.setData(dapanService.getYeWuResult3(aa.dt, aa.dt2, aa.biz, aa.metaIds, params.userID));
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
    /**
     *  [PC使用] 大盘展示
     * @param params 请求参数  dt时间， dt2结束时间， biz业务: [zp,ganji,chinahr]
     * @param result 请求结果
     * @return 获取所有的指标大盘情况
     */
    @RestGrpcMapping("/listDapanSeriesAll.do")
    public NTSTATUS listDapanSeriesAll(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            result.setData(dapanService.listDapanSeriesAll(aa.dt, aa.dt2, aa.biz));
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }
    /**
     * [小程序] 具体指标的序列(全屏)
     * @param params 参数
     * @param result 返回结果
     * @return 执行状态
     */
    @RestGrpcMapping("/getQuotaList.do")
    public NTSTATUS getQuotaList(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param dto = params.driverObject(Param.class);
            result.setData(dapanService.getQuotaList(dto.getDt(), dto.getDt2(), dto.getPageName(), dto.getId()));
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    /**
     *  [PC使用] 大盘展示
     * @param params 请求参数  dt时间， dt2结束时间， biz业务: [zp,ganji,chinahr]
     * @param result 请求结果
     * @return 状态返回
     */
//    @RestGrpcMapping("/listZhibiaoDashboard.do")
//    public NTSTATUS listZhibiaoDashboard(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("request params: {}", params);
//        try {
//            Param aa = params.driverObject(Param.class);
//            result.setData(dapanService.listZhibiaoDashboard(aa.dt));
//            return NTSTATUS.SUCCESS_STATUS();
//        }catch (BusinessException e) {
//            log.warn("Exception: {}. {}",e.toString(), params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        }catch (Exception e) {
//            log.error("Exception: {}. {}", e.toString(), params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }




//    @RestGrpcMapping("/getYeWuResult.do")
//    public NTSTATUS getYeWuResult(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("request params: {}", params);
//        try {
//            Param aa = params.driverObject(Param.class);
//            result.setData(dapanService.getYeWuResult(aa.getDt(), aa.getDt2(), aa.getPageName()));
//            return NTSTATUS.SUCCESS_STATUS();
//        }catch (BusinessException e) {
//            log.warn("Exception: {}. {}",e.toString(), params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        }catch (Exception e) {
//            log.error("Exception: {}. {}", e.toString(), params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }

    /**
     * [小程序使用]获取指标列表
     * @param params 参数
     * @param result 结果
     * @return 指标详情
     */
    @RestGrpcMapping("/getWxExpList.do")
    public NTSTATUS getWxExpList(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            result.setData(dapanService.getWxExpList(aa.dt, aa.dt2, aa.pageName, aa.appDiv, aa.zgroup));
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    /**
     * [小程序使用]获取指定指标趋势图
     * @param params 参数
     * @param result 结果
     * @return 指标详情
     */
    @RestGrpcMapping("/metricSeries.do")
    public NTSTATUS metricSeries(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            WxMiniProDto dto = params.driverObject(WxMiniProDto.class);
            result.setData(dapanService.metricSeries(dto.dt, dto.dt2, dto.code, dto.pageName));
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }




    @Data
    public static class WxMiniProDto {

        public String dt;
        public String dt2;

        public String biz;
        /** 应用板块 */
        public String appdiv;
        /** influx 表名称 */
        public String table;
        /** influx 业务指标标签 */
        public String tag;
        public String dim;

        public String database;
        public String dtType;
        public String pageName;
        public String code;
        /** 用于给用户发送模板消息的ID */
        String[] formId;
        /** 微信小程序用户的openid */
        String openid;

    }

    // [小程序使用] influxDB读取数据
    @RestGrpcMapping("/series.do")
    public NTSTATUS series(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            WxMiniProDto dto = params.driverObject(WxMiniProDto.class);
            result.setData(dapanService.listSeries(dto.dt, dto.dt2, dto.tag, dto.table, dto.dim));
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }


    /**
     * 【小程序使用】实时数据序列
     * @param params 参数
     * @param result 返回结果
     * @return 执行状态
     */
    @RestGrpcMapping("/kanbanList.do")
    public NTSTATUS kanbanList(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            WxMiniProDto dto = params.driverObject(WxMiniProDto.class);
            result.setData(dapanService.listRtKanban(dto.dt, dto.dt2, dto.biz, dto.appdiv, null));
            return NTSTATUS.SUCCESS_STATUS();
        }catch (BusinessException e) {
            log.warn("Exception: {}. {}",e.toString(), params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        }catch (Exception e) {
            log.error("Exception: {}. {}", e.toString(), params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }





}
